﻿using UnityEngine;
using UnityEngine.UI;

namespace DanMuHelper.Tools
{
    /// <summary>
    /// 菱形UI
    /// </summary>
    public class DiamondImage : Image
    {
        private int vertexCount; // 菱形的顶点数量

        protected override void OnPopulateMesh(VertexHelper vertexHelper)
        {
            vertexCount = 4; // 菱形有4个顶点

            // 清除之前的顶点信息和三角面信息
            vertexHelper.Clear();
            float width = rectTransform.rect.width;
            float height = rectTransform.rect.height;

            // 创建菱形顶点
            UIVertex topVertex = new UIVertex
            {
                position = new Vector2(0, height * 0.5f),
                color = color,
                uv0 = new Vector2(0.5f, 1.0f) // UV坐标
            };
            UIVertex rightVertex = new UIVertex
            {
                position = new Vector2(width * 0.5f, 0),
                color = color,
                uv0 = new Vector2(1.0f, 0.5f)
            };
            UIVertex bottomVertex = new UIVertex
            {
                position = new Vector2(0, -height * 0.5f),
                color = color,
                uv0 = new Vector2(0.5f, 0.0f)
            };
            UIVertex leftVertex = new UIVertex
            {
                position = new Vector2(-width * 0.5f, 0),
                color = color,
                uv0 = new Vector2(0.0f, 0.5f)
            };

            // 添加顶点到vertexHelper
            vertexHelper.AddVert(topVertex);
            vertexHelper.AddVert(rightVertex);
            vertexHelper.AddVert(bottomVertex);
            vertexHelper.AddVert(leftVertex);

            // 添加三角面信息，创建两个三角形
            vertexHelper.AddTriangle(0, 1, 2); // 上半部分三角形
            vertexHelper.AddTriangle(0, 2, 3); // 下半部分三角形
        }
    }
}
